<?php
/**
 * 公共方法请求类
 *
 */
namespace Home\Model;
use Think\Model;
class FuncModel extends Model {

	public function __construct()
	{
		parent::__construct();
	}
	
	

	/**
	 * @param array $arr 失败结果返回
	 * @author xun
	 * @return string
	 */
	public function ajaxFail($arr, $code=1, $status='failure')
	{
		$result['code'] = $code;
		$result['status'] = $status;
		$result['data'] = $arr;
		if (isset($_GET['debug_ajax'])) {
			print_r($result);
			exit;
		}
		$this->ajaxReturn($result);
	}
	
	
	/**
	 * @param array $arr 成功结果返回
	 * @author xun
	 * @return string
	 */
	public function ajaxSucc($arr, $code=0, $status='success')
	{
		$result['code'] = $code;
		$result['status'] = $status;
		$result['data'] = $arr;
		if (isset($_GET['debug_ajax'])) {
			print_r($result);
			exit;
		}
		$this->ajaxReturn($result);
	}
	


	/**
	 * @param array $arr 下载地址列表
	 * @author xun
	 * @return string
	 *  
	 	$Func = new \Home\Model\FuncModel();
        $total = 105;
        $link = '';
        $arr = array();
        for($i=0; $i<$total; $i++)
        {
        	$link = 'http://stc.weimob.com/img/sns/emotion/' . $i . '.gif';
        	$arr = array_merge($arr, array($i.'.gif'=>$link));
        }
        $Func->saveWebFiles($arr);
        exit;
	 * 
	 * 
	 */
	public function saveWebFiles($arr, $over_written=false, $exc_time=60)
	{
		ini_set('max_execution_time', $exc_time);
		$result = array('succ'=>array(), 'fail'=>array());
		foreach($arr as $k=>$v)
		{
			if( file_exists($k) && (!$over_written || is_writable($k)))
			{
				$result['fail'] = array_merge($result['fail'], array($k));
			}
			else 
			{
				$file_str = file_get_contents($v);
				$fp = fopen($k, 'w');
				fwrite($fp, $file_str);
				fclose($fp);
				$result['succ'] = array_merge($result['succ'], array($k));
					
			}
		}
		return $result;
	}
}
